// pages/search/search.js
const getToken = require('../../utils/get_token.js');
const formatTime = require('../../utils/util.js');
const feedbackApi = require('../common/showToast.js');
let openId = '', memberId = '', pageNo = 1;
let cookie, token;
const indexList = (that) => {//猜你喜欢
  that.setData({
    hidden: false
  })
  let params = ['shop/pingTuan/guessYouLike', {"openId":openId,"memberId":memberId,"pageSize":pageNo,"seeType":2,"pinTuanGoodsId":"","orderSn":""}]
  getToken.getData(function (res) {
    console.log(res.data)
    let data = res.data;
    if (data.rows.length > 0) {
      for (let i = 0; i < data.rows.length; i++) {
       // data.rows[i].goodsImageStore = data.rows[i].goodsImageStore.substring(1, data.rows[i].goodsImageStore.length - 1);
        data.rows[i].goodsImageStore = JSON.parse(data.rows[i].goodsImageStore);
        data.rows[i].img = 'https://www.globalhexi.com/' + data.rows[i].goodsImageStore[0].path + '/small/' + data.rows[i].goodsImageStore[0].id + '.jpg';
        data.rows[i].intPinPrice = (data.rows[i].intPinPrice / 100).toFixed(2);
        data.rows[i].intPrice = (data.rows[i].intPrice / 100).toFixed(2);
        that.data.list.push(data.rows[i]);
      }
    }
    that.setData({
      list: that.data.list,
      hidden: true
    })
  }, ...params)
};
const search = (that) => {//搜索
  that.setData({
    hidden: false
  })
  wx.request({
    header: { 'content-type': 'application/x-www-form-urlencoded' },
    url: 'https://interface.globalhexi.cn/shop/pingTuan/searchPingTuanGoodsEs',
    data: { "tuanSetType": 1, "keyWord": that.data.keyWord, "pageNo": pageNo },
    method: 'POST',
    success: function (res) {
      console.log(res.data)
      let datas = res.data.rows;
      if (datas.length > 0) {
        that.data.shopShow = true;
        for (let i = 0; i < datas.length; i++) {
          datas[i].img = 'http://www.globalhexi.com/' + datas[i].productImage;
          datas[i].intPinPrice = (datas[i].intPinPrice / 100).toFixed(2);
          datas[i].intPrice = (datas[i].intPrice / 100).toFixed(2);
          that.data.list.push(datas[i]);
        }
      } else {
        if (pageNo == 1)//没有搜索到
        {
          that.data.shopShow = false;
        }
      }
      that.setData({
        shopShow: that.data.shopShow,
        list: that.data.list,
        hidden: true
      })
    }
  })
};
Page({

  /**
   * 页面的初始数据
   */
  data: {
		boolT: true,
    shopShow:true,
    hidden:true,
		show:false,
    keyWord:'',
    list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
		let that = this;
    memberId = wx.getStorageSync('memberId') || '';
    openId = wx.getStorageSync('openId') || '';
    cookie = wx.getStorageSync('cookie') || '';
    token = wx.getStorageSync('token') || '';
    that.data.keyWord = options.id;
    that.setData({
      keyWord: that.data.keyWord
    })
    that.data.list = [];
    pageNo = 1;
    search(that);
  },
	search:function(e){//查找
		let that = this;
		e.detail.value= e.detail.value.replace(/\s+/g, "");
		if (e.detail.value!='')
		{
      that.data.list=[];
      pageNo = 1;
      that.data.keyWord = e.detail.value;
      search(that);
		} else {
			feedbackApi.showToast({ title: '请输入搜索内容', mask: false })//调用 showToast
		}
	},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (e) {
		var that=this;
		
  },
	enter: function (e) {
    let that = this;
    if (that.data.boolT) {
      that.data.boolT = false;
      wx.navigateTo({
        url: '../paired_detail/paired_detail?scene=' + e.currentTarget.dataset.id,
        success: function () {
          setTimeout(function () {
            that.data.boolT = true;
          }, 500)
        }
      })
    }
	},
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
   let that=this;
    pageNo++;
    if (that.data.shopShow)
    {
      search(that);
    }else{
      indexList(that);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})